//
//  B_ManagerAddressVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/19.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_ManagerAddressVC.h"
#import "B_ReleaseFooterView.h"
#import "B_AddressListTVC.h"
#import "B_AddAddressVC.h"
#import "B_AddressListModel.h"
#import "B_AddressManagerVC.h"

@interface B_ManagerAddressVC ()


@property (nonatomic, strong) BaseTableView * mainView;

@property (nonatomic, strong) B_ReleaseFooterView * addFooter;

@end

@implementation B_ManagerAddressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    WeakSelf(weSelf, self);
    [self setTitle:@"我的收货地址"];
    self.view = self.mainView;
    self.dataArray = [NSMutableArray array];
    [self getData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addressGetData) name:@"addressGetData" object:nil];
    [self setRightTextBarButtonItemWithFrame:CGRectZero title:@"管理" titleColor:[UIColor color_text_light_gray] backImage:@"" selectBackImage:@"" action:^(GUIButton *button) {
        //TODO:管理地址
        B_AddressManagerVC * manager = [[B_AddressManagerVC alloc] init];
        [weSelf pushNextViewController:manager];
        
    }];
}

- (void) addressGetData {
    [self getData];
}

- (BaseTableView *)mainView {
    if (!_mainView) {
        WeakSelf(weSelf, self);
        _mainView = [[BaseTableView alloc] init];
        [_mainView addSubview:self.addFooter];
        [self.addFooter mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mainView.mas_left);
            make.right.equalTo(self.mainView.mas_right);
            make.bottom.equalTo(self.mainView.mas_bottom);
            make.height.equalTo(@(80));
        }];
        CGFloat bottom = SCREEN_HEIGHT > 800 ? (80 + 24) : 80;
        [_mainView setTableViewFrameWithTop:5 left:0 right:0 bottom:-bottom];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_AddressListTVC" bundle:nil] forCellReuseIdentifier:@"B_AddressListTVC"];
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            return weSelf.dataArray.count;
        };
        _mainView.heightForRowBlock = ^CGFloat(UITableView *tableView, NSIndexPath *indexPath) {
            return 90;
        };
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            B_AddressListModel * model = weSelf.dataArray[indexPath.row];
            B_AddressListTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_AddressListTVC" forIndexPath:indexPath];
            cell.B_nameLabel.text = model.userName;
            if ([model.isDefault isEqualToString:@"1"]) {
                cell.B_defaultView.hidden = NO;
            } else {
                cell.B_defaultView.hidden = YES;
            }
            cell.B_phoneLabel.text = model.userPhone;
            cell.B_addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@ %@", model.province, model.city, model.district, model.location];
            
            return cell;
        };
        _mainView.didSelectRow = ^(UITableView *tableView, NSIndexPath *indexPath) {
            
        };
        
    }
    return _mainView;
}

- (B_ReleaseFooterView *)addFooter {
    if (!_addFooter) {
        WeakSelf(weSelf, self);
        _addFooter = [B_ReleaseFooterView initB_ReleaseFooterView];
        [_addFooter.B_SureButton setTitle:@"添加新地址" forState:UIControlStateNormal];
        _addFooter.sureActionBlock = ^{
            //TODO:添加新地址
            B_AddAddressVC * add = [[B_AddAddressVC alloc] init];
            add.type = B_AddressTypeNew;
            [weSelf pushNextViewController:add];
        };
    }
    return _addFooter;
}

- (void) getData {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/userAddress/selectUserAddressList/%@", getUserId()] parameters:nil isCache:YES succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            [self.dataArray removeAllObjects];
            for (NSDictionary * dictionary in data[@"values"]) {
                B_AddressListModel * model = [B_AddressListModel mj_objectWithKeyValues:dictionary];
                [weSelf.dataArray addObject:model];
            }
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"addressGetData" object:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
